<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <template id="sub-component">
        <div>
            <h2>子组件</h2>
            <button @click="handleClick">我要传参</button>
        </div>
    </template>
    <main id="app">
        <sub-component @my-event="handleMyEvent"></sub-component>
        <p>name: {{name}}</p>
        <p>age: {{age}}</p>
    </main>
</body>
<script src="../../vue.js"></script>
<script>
    Vue.component("sub-component",{
        template: "#sub-component",
        methods: {
            handleClick() {
                this.$emit("my-event",{name: "Hydra",age: 35})
            }
        }
    })

    new Vue({
        el: "#app",
        data() {
            return {
                name: "",
                age: ""
            }
        },
        methods: {
            handleMyEvent(param) {
                console.log("子组件传参：",param);
                this.name = param.name;
                this.age = param.age;
            }
        }
    })
</script>
</html>